<template>
  <div class="h100 df fdc jcsb">
    <div class="f1 h0 df fdc">
      <div class="df jcsb aic">
        <p>打招呼</p>
        <el-button @click="createPrompt">新建prompt</el-button>
      </div>
      <el-table class="f1" :data="tableData">
        <el-table-column prop="id" label="PromptID"></el-table-column>
        <el-table-column prop="prompt" label="Prompt内容"></el-table-column>
        <el-table-column
          prop="updateDateStr"
          label="创建时间"
        ></el-table-column>
        <el-table-column
          prop="updateDateStr"
          label="更新时间"
        ></el-table-column>
        <el-table-column label="状态">
          <template #default="scope">
            <p
              :style="{
                color: scope.row.status == 1 ? '#409EFF' : '#606266',
              }"
            >
              {{ scope.row.statusText }}
            </p>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <div v-show="scope.row.status != 1">
              <el-button @click="pushOnline(scope.row)" link
                >推到线上</el-button
              >
              <el-button @click="editPrompt(scope.row)" link>编辑</el-button>
              <el-button @click="deletePe(scope.row)" link>删除</el-button>
            </div>
            <div v-show="scope.row.status == 1">-</div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="height: 40px; margin-top: 20px"
    />
  </div>
</template>

<script setup>
import { ref, watch, onMounted } from "vue";
import { peList, peStatus, delPe } from "@/apis/pe";
import { useCategoryStore } from "@/store";
import { transDate } from "@/utils/transforDate";
import { ElMessage, ElMessageBox } from "element-plus";

const tableData = ref([]);
const form = ref({
  content: "",
  status: "",
  remark: "",
});
const currentPage = ref(1);
const pageSize = ref(20);
const total = ref(0);

const props = defineProps({
  data: String,
});
const emit = defineEmits(["backHello"]);
const store = useCategoryStore();

onMounted(() => {
  if (props.data == "second") {
    getData();
  }
});

const getData = async () => {
  try {
    let data = {};
    data.page = currentPage.value;
    data.page_size = pageSize.value;
    data.category = "greet";
    data.sub_category = "";
    let res = await peList(data);
    if (res.data.code == 1002) {
      tableData.value = res.data.data.items;
      total.value = res.data.data.total;
      tableData.value = tableData.value.map((item) => {
        item.createDateStr = transDate(item.created_at);
        item.updateDateStr = transDate(item.updated_at);
        item.statusText = item.status == 0 ? "未生效" : "线上生效中";
        return item;
      });
    }
  } catch (e) {
    console.log(e);
  }
};

const handleSizeChange = (e) => {
  getData();
};

const handleCurrentChange = (e) => {
  getData();
};

const createPrompt = () => {
  store.setCategroyMethod("create");
  store.setCurrentCateGroy("");
  store.setCurrentSubCategroy("");
  emit("backHello", { active: 2 });
  store.setCurrentCateGroy("greet");
};

const editPrompt = (e) => {
  store.setCategroyMethod("edit");
  store.setCurrentCateGroy("greet");
  store.setCurrentSubCategroy("");
  emit("backHello", { active: 2 });
  store.setEditData(e);
};
const pushOnline = (e) => {
  ElMessageBox.confirm("是否确定推到线上？", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  })
    .then(async () => {
      let data = {};
      data.id = JSON.stringify(e.id);
      data.status = 1;
      try {
        let res = await peStatus(data);
        if (res.data.code == 200) {
          ElMessage.success("成功");
          getData();
        }
      } catch (e) {
        console.log(e, "eee");
      }
    })
    .catch(() => {
      ElMessage.info("取消");
    });
};

const deletePe = (e) => {
  ElMessageBox.confirm("确定删除此条？", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  }).then(async () => {
    try {
      let data = {};
      data.id = e.id;
      let res = await delPe(data);
      if (res.data.code == 200) {
        ElMessage.success("删除成功");
        getData();
      }
    } catch (e) {}
  });
};
watch(
  () => props.data,
  (newVal) => {
    if (newVal == "second") {
      getData();
    }
  }
);

watch(
  () => store.currentType,
  (newVal) => {
    if (newVal == "greet") {
      getData();
      store.setCurrentType("");
    }
  }
);
</script>

<style lang="scss" scoped></style>
